/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ads.hash;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 *
 * @author framet-lap
 */
public class HashToolBox {
    public static ArrayList<Participant> createParticipantsFromList(String filename) throws IOException, TimeFormatException{
        FileInputStream fstream = new FileInputStream(filename);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        ArrayList<Participant> participants = new ArrayList<>();
        
        while ((strLine = br.readLine()) != null) {
            //1;RÃƒÂ¶thlin;Viktor;74;Magglingen/Macolin;2:11:05;M20
            String[] participant = strLine.split(";");
            if(participant.length==7){
                int id = Integer.valueOf(participant[0]);
                String name = participant[1];
                String firstname = participant[2];
                int birthyear = Integer.valueOf(participant[3]);
                String location = participant[4];
                double timeInMillis = timeConverter(participant[5]);
                String category = participant[6];
                
                Participant p = new Participant(id, firstname, name, birthyear, location, timeInMillis, category);
                participants.add(p);
            }
        }
        return participants;
    }
    
    private static double timeConverter(String value) throws TimeFormatException{
        if(value.matches("\\d{1,2}:\\d{2}:\\d{2}")){
            double time = 0;
            String[] s = value.split(":");
            time += Integer.valueOf(s[2])*1000;
            time += Integer.valueOf(s[1])*1000*60;
            time += Integer.valueOf(s[0])*1000*60*60;
            return time;
        }
        throw new TimeFormatException();
    }

    public static HashTable createHashTable(ArrayList<Participant> participants, int size) throws HashTableOverflowException {
        HashTable hashTable = new HashTable(size);
        for(Participant p:participants){
            hashTable.add(p);
        }
        return hashTable;
    }
}
